package fr.utbm.aiechat.structures;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import fr.utbm.aiechat.R;

public class ConversationAdapter extends ArrayAdapter<Conversation> implements android.widget.AdapterView.OnItemClickListener
{
	private Context context;
	private int textViewResourceId;
	
	public ConversationAdapter(Context context, int textViewResourceId)
	{
		super(context, textViewResourceId);
		this.context = context;
		this.textViewResourceId = textViewResourceId;
	}

	@Override
    public View getView(int position, View convertView, ViewGroup parent)
	{
        View row = convertView;
        ConversationHolder holder = null;
       
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(textViewResourceId, parent, false);
           
            holder = new ConversationHolder();
            holder.ConvName = (TextView)row.findViewById(R.id.convName);
           
            row.setTag(holder);
        }
        else
        {
            holder = (ConversationHolder)row.getTag();
        }
        
        Conversation conv = getItem(position);
        holder.ConvName.setText(conv.getName());
       
        return row;
    }
	
    static class ConversationHolder
    {
        TextView ConvName;
    }

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	{
		System.out.println(this.getItem(position).getName());
	}
}
